package com.steven.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarBetweenDate {

	public static int getDaysBetween(java.util.Calendar d1, java.util.Calendar d2) {
		if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
			java.util.Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		int days = d2.get(java.util.Calendar.DAY_OF_YEAR)
				- d1.get(java.util.Calendar.DAY_OF_YEAR);
		int y2 = d2.get(java.util.Calendar.YEAR);
		if (d1.get(java.util.Calendar.YEAR) != y2) {
			d1 = (java.util.Calendar) d1.clone();
			do {
				days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
				d1.add(java.util.Calendar.YEAR, 1);
			} while (d1.get(java.util.Calendar.YEAR) != y2);
		}
		return days;
	}

    public static  String  getDateByWorkDay(String startDateString , int days){
        String  endDateString ="";
        startDateString= startDateString.substring(0,10)+" 23:59:59";
        SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar  calendar =  Calendar.getInstance();
        Date startDate = null;
        try {
            startDate = sdf.parse(startDateString);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        calendar.setTime(startDate);


        for(int i =0;i<days;i++){

           int  weekday =  calendar.get(Calendar.DAY_OF_WEEK);

            if( weekday==6 )     {   //星期五
                calendar.add(Calendar.DAY_OF_YEAR,3);
            }else if(weekday==7){          //星期六
                calendar.add(Calendar.DAY_OF_YEAR,2);
            } else {
                calendar.add(Calendar.DAY_OF_YEAR,1);
            }
            System.out.println(i+1+":"+weekday+" : " + sdf.format(calendar.getTime())  );
        }



        endDateString=   sdf.format(calendar.getTime())       ;
        System.out.println(endDateString);


          return               endDateString;

    }
    public static String getWeekdaysByEndAndStartDatetime(String endDateTimeStr,
                                                          String startDateTimeStr)
    {
        Date startDate = null;
        Date endDate  =null;
        int  weekdays = 0;
        SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            startDate = sdf.parse(startDateTimeStr);
            endDate = sdf.parse(endDateTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        Calendar  calendar =  Calendar.getInstance();
        calendar.setTime(startDate);

         while (startDate.before(endDate)){
             int  weekday =  calendar.get(Calendar.DAY_OF_WEEK);
             System.out.println(weekday+":"+weekdays);
            if(weekday==7){          //星期六
                weekdays++;
                System.out.println(weekday+":"+weekdays);
             }
             if(weekday==1){
                 weekdays++;
                 System.out.println(weekday+":"+weekdays);

            }
             calendar.add(Calendar.DAY_OF_YEAR, 1);
                startDate =calendar.getTime();
         }



        return  weekdays+"";



    }
	public static void main(String[] args) {
		/*SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar c = Calendar.getInstance();
		Date birthday=null;
		try {
			birthday = sdf.parse("2012-03-16");
		 
		} catch (ParseException e) {

			e.printStackTrace();
		}
		c.setTime(birthday);

		System.out.println(getDaysBetween(c,Calendar.getInstance()));
		*/
      //  getDateByWorkDay("2012-10-22 17:07:33",1);

        /*** 定制每日8：30：00执行方法
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND, 0);
        Date date = calendar.getTime();
        SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(sdf.format(date));
         ***/

    /*    String  start = "2012-10-22" ;
        String  end ="2012-10-26"    ;


        System.out.println(getWeekdaysByEndAndStartDatetime(end,start));

*/

        if ( "2012-10-12 12:00:00".compareTo("2012-10-12 10:00:00") > 0
                && "2012-10-12 09:00:00".compareTo("2012-10-12 23:59:59") <= 0) {
            System.out.println("12");
        }


	}

}
